package com.example.itemdecoration.decoration;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.provider.CalendarContract;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;

import com.example.itemdecoration.R;
import com.example.itemdecoration.UIUtils;

/**
 * Created by TAOTAO.
 * Date: 2020/6/10
 * Time: 9:21
 */
public class MyDecoration extends RecyclerView.ItemDecoration {

    private final Context mContext;
    private Paint mPaint;
    private Paint mTextPaint;
    private Bitmap bitmap;
    private Paint mPaintBord;



    public MyDecoration(Context context) {
        mContext = context;
        mPaint = new Paint();
        mPaint.setColor(Color.WHITE);

        mTextPaint = new Paint();
        mTextPaint.setColor(Color.BLACK);
        mTextPaint.setTextSize(UIUtils.sp2px(18));

        mPaintBord=new Paint();
        mPaintBord.setColor(mContext.getColor(R.color.colorAccent));

        bitmap = BitmapFactory.decodeResource(mContext.getResources(), R.mipmap.ql_icon_main_hot);
    }

    //设置ItemView的内嵌偏移长度（inset）
    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        super.getItemOffsets(outRect, view, parent, state);
        int position = parent.getChildAdapterPosition(view);
        if (position == 1) {
            outRect.set(0, UIUtils.dp2px(60), 0, UIUtils.dp2px(1));
        } else {
            outRect.set(0, 0, 0, UIUtils.dp2px(1));
        }
    }

    // 在子视图上设置绘制范围，并绘制内容
    // 绘制图层在ItemView以下，所以如果绘制区域与ItemView区域相重叠，会被遮挡
    @Override
    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
        super.onDraw(c, parent, state);
        for (int i = 0; i < parent.getChildCount(); i++) {
            View child = parent.getChildAt(i);
            if (parent.getChildAdapterPosition(parent.getChildAt(i)) == 1) {
                c.drawRect(0,child.getTop(),child.getRight(),0,mPaintBord);
                c.drawRect(0,child.getTop()-UIUtils.dp2px(1),child.getRight(),0,mPaint);

                c.drawBitmap(bitmap, UIUtils.dp2px(18), child.getTop() - (UIUtils.dp2px(60) / 2 + bitmap.getHeight() / 2), mPaint);
                String str = "热门内容";
                c.drawText(str, UIUtils.dp2px(24) + bitmap.getWidth(), child.getTop() + getTextHeight(str) - UIUtils.dp2px(60) + bitmap.getHeight(), mTextPaint);
            }
        }
    }

    public float getTextHeight(String text) {
        return mTextPaint.descent() - mTextPaint.ascent();
    }

    //同样是绘制内容，但与onDraw（）的区别是：绘制在图层的最上层
    @Override
    public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
        super.onDrawOver(c, parent, state);
        for (int i = 0; i < parent.getChildCount(); i++) {
            View child = parent.getChildAt(i);
            int index = parent.getChildAdapterPosition(child);

            if (index > 0 && child.getTop() < UIUtils.dp2px(60)) {
                int left =  0;
                int top = UIUtils.dp2px(60);
                int right = child.getRight();
                int bottom = 0;

                c.drawRect(left, top, right, bottom, mPaint);
                c.drawBitmap(bitmap, UIUtils.dp2px(18), top / 2-bitmap.getWidth()/2, mPaint);
                String str = "热门内容";
                c.drawText(str, UIUtils.dp2px(24)+ bitmap.getWidth(), getTextHeight(str)/2+top / 2 , mTextPaint);
            }
        }
    }

}
